//
//  ViewController.m
//  03-28-20606ApiDemo
//
//  Created by student on 16/3/29.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "NetManager.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *idTextField;

@property (weak, nonatomic) IBOutlet UITextView *resultTextView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activity;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(id)sender {
    
    
    //第一种
//    NSString *httpUrl = @"http://apis.baidu.com/apistore/idservice/id";
//    NSString *httpArg = [NSString stringWithFormat:@"id=%@",self.idTextField.text];
//    [self request:httpUrl withHttpArg:httpArg];
    
    
    //第二种
//    self.resultTextView.text=[NetManager requestInfoByPersonId:self.idTextField.text];
    
    
    //第三种
    [self.activity startAnimating];
    [NetManager requestInfoByPersonId:self.idTextField.text successBlock:^(NSString *result) {
        self.resultTextView.text=result;
        [self.activity stopAnimating];
    } failBlock:^(NSError *error) {
        self.resultTextView.text=[error localizedDescription];
        [self.activity stopAnimating];
    }];
    
}

//**********第一种*********//

//-(void)request:(NSString *)httpUrl withHttpArg: (NSString *)httpArg
//{
//    //网址字符串
//    NSString *urlStr = [[NSString alloc]initWithFormat:@"%@?%@",httpUrl,httpArg];
//    //网址
//    NSURL *url  = [NSURL URLWithString:urlStr];
//    
//    //请求
//    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//    //GET请求
//    [request setHTTPMethod:@"GET"];
//    
//    //设置请求头
//    
//    [request addValue: @"a47710812a161be75ac6713a123dcbde" forHTTPHeaderField: @"apikey"];
//    [NSURLConnection sendAsynchronousRequest: request
//                                       queue: [NSOperationQueue mainQueue]
//                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
//                               if (error) {
//                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
//                               } else {
//                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
//                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//                                   NSLog(@"HttpResponseCode:%ld", responseCode);
//                                   NSLog(@"HttpResponseBody %@",responseString);
//                                   
//                                   NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//                                   NSDictionary *dict1=dict[@"retData"];
//                                   self.resultTextView.text=[NSString stringWithFormat:@"生日:%@\n地址:%@\n性别:%@",dict1[@"birthday"],dict1[@"address"],dict1[@"sex"]];
//                               }
//                           }];
//
//    
//}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    [self.resultTextView endEditing:YES];
}

@end
